package a2dp.vol.chat;

import java.util.Vector;

import a2dp.vol.chat.MyApplication;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	Resources res;
	private MyApplication application;
	private ServiceDB sDB;
	private Vector<ServiceItem> vec;
	ArrayAdapter<String> ladapt;
	String[] lstring = null;
	ListView lvl = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		res = getResources();
		setContentView(R.layout.front);

		ComponentName comp = new ComponentName("a2dp.vol.chat", "MainActivity");
		PackageInfo pinfo;
		String ver = null;
		try {
			pinfo = getPackageManager()
					.getPackageInfo(comp.getPackageName(), 0);
			ver = pinfo.versionName;
		} catch (NameNotFoundException e) {
			// Log.e(LOG_TAG, "error" + e.getMessage());
		}

		setTitle(res.getString(R.string.app_name) + " Version: " + ver);
		
		Button btnLogin = (Button) findViewById(R.id.connectButton);
		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent();
				i.setClass(a2dp.vol.chat.MainActivity.this, ConnectActivity.class);
				startActivity(i);
			}
		});

		Button btnExit = (Button) findViewById(R.id.exitButton);
		btnExit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				stopService(new Intent(a2dp.vol.chat.MainActivity.this,
						NChatService.class));
				sDB.getDB().close();
				finish();
			}
		});
		// get "Application" object for shared state or creating of expensive
		// resources - like DataHelper
		// (this is not recreated as often as each Activity)
		this.application = (MyApplication) this.getApplication();
		this.sDB = new ServiceDB(application);
		lstring = new String[] {res.getString(R.string.Empty)};
		this.ladapt = new ArrayAdapter<String>(application,android.R.layout.simple_list_item_1, lstring);
		this.lvl = (ListView) findViewById(R.id.listViewServices);
		this.lvl.setAdapter(ladapt);
		lvl.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (vec.isEmpty())
					return;
				final ServiceItem si = vec.get(position);
				final ServiceItem si2 = sDB.getSI(si.getAddress());
				Intent i = new Intent();
				i.setClass(a2dp.vol.chat.MainActivity.this, ConnectActivity.class);
				i.putExtra("key", si2.getKey());
				startActivity(i);
				
			}
			
		});
		reload();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#finish()
	 */
	@Override
	public void finish() {
		sDB.getDB().close();
		super.finish();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		reload();
		super.onResume();
	}

	private void reload(){
		sDB.getDB().close();
		if(!sDB.getDB().isOpen() && !sDB.getDB().isDbLockedByCurrentThread() && !sDB.getDB().isDbLockedByOtherThreads())
			this.sDB = new ServiceDB(application);
		vec = sDB.listAll();
		if(vec.isEmpty()){
			lstring = new String[] {res.getString(R.string.Empty)};
		}
		else{
			int size = vec.size();
			lstring = new String[size];
			for(int i=0; i < size; i++){
				lstring[i] = vec.get(i).getSname();
			}
			
		}
		
		a2dp.vol.chat.MainActivity.this.ladapt = new ArrayAdapter<String>(application,android.R.layout.simple_list_item_1, lstring);
		a2dp.vol.chat.MainActivity.this.lvl.setAdapter(ladapt);
		a2dp.vol.chat.MainActivity.this.ladapt.notifyDataSetChanged();
		a2dp.vol.chat.MainActivity.this.lvl.invalidateViews();
		a2dp.vol.chat.MainActivity.this.lvl.forceLayout();
	}

}